<template>
  <page-header-wrapper>
    <a-spin :spinning="searchLoading">
      <GlobalSearch :list="searchList" @search="handleSearch" @reset="handleReset"> </GlobalSearch>
    </a-spin>
    <a-card :bordered="false" class="m-b-24">
      <s-table
        ref="table"
        size="default"
        rowKey="ID"
        :columns="columns"
        :data="loadData"
        :pagination="{ showTotal: (total) => `共 ${total} 条` }"
      >
        <span slot="index" slot-scope="text, record, index">{{ index + 1 }} </span>
        <div slot="action" slot-scope="text, record">
          <y-button type="link" @click="handleDetail(record)">查看</y-button>
        </div>
      </s-table>
    </a-card>
    <a-card :bordered="false" class="m-b-24" >
      <template #title>
        <a-icon type="pie-chart" theme="filled" />
        <span class="m-l-16">数据统计</span>
      </template>
      <a-row :gutter="[24, 24]">
        <a-col :span="24">
          <house-pie :strContent="queryParam.strContent"/>
        </a-col>
      </a-row>
    </a-card>
    <a-card :bordered="false" >
      <template #title>
        <a-icon type="pie-chart" theme="filled" />
        <span class="m-l-16">数据统计</span>
      </template>
      <a-row :gutter="[24, 24]">
        <a-col :span="12">
          <distribution-pie/>
        </a-col>
        <a-col :span="12">
          <sign-bar/>
        </a-col>
      </a-row>
    </a-card>
    <detail :visible.sync="visibleDetail" :data.sync="contract"></detail>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis, signContract } from '@/components'
import { getContractStatistics } from '@/api/followUpManagement'
import { getDropdownList } from '@/api/common'
import detail from '@/components/siginContract/modalContract.vue'
import moment from 'moment'
import distributionPie from './components/distribution.vue'
import housePie from './components/house.vue'
import signBar from './components/sign'
export default {
  components: {
    STable,
    Ellipsis,
    signContract,
    detail,
    distributionPie,
    signBar,
    housePie
  },
  data () {
    return {
      searchLoading: false,
      contract: {},
      visibleDetail: false,
      queryParam: {
        strContent: {}
      },
      searchList: [
        {
          label: '合同类别',
          fields: 'PactClass',
          type: 'select',
          props: {
            placeholder: '请选择合同类别',
            options: []
          }
        },
        {
          label: '合同状态',
          fields: 'Status',
          type: 'select',
          props: {
            placeholder: '请选择合同状态',
            options: []
          }
        },
        {
          label: '合同编号',
          fields: 'PactNo',
          type: 'input',
          props: {
            placeholder: '请输入合同编号'
          }
        },
        {
          label: '合同名称',
          fields: 'UnitName',
          type: 'input',
          props: {
            placeholder: '请输入合同名称'
          }
        },
        {
          label: '乙方姓名',
          fields: 'Name',
          type: 'input',
          props: {
            placeholder: '请输入乙方姓名'
          }
        }
      ],
      columns: [
        {
          title: '序号',
          align: 'center',
          width: '80px',
          scopedSlots: { customRender: 'index' }
        },
        {
          title: '合同编号',
          align: 'center',
          dataIndex: 'PactNo'
        },
        {
          title: '申请编号',
          align: 'center',
          dataIndex: 'ApplyNo'
        },
        {
          title: '乙方租户',
          align: 'center',
          dataIndex: 'Name'
        },
        {
          title: '合同类型',
          align: 'center',
          dataIndex: 'PactClass'
        },
        {
          title: '合同名称',
          align: 'center',
          dataIndex: 'UnitName'
        },
        {
          title: '合同状态',
          align: 'center',
          dataIndex: 'Status',
          customRender: (text) => {
            const textColor = new Map([
              ['已签', 'text-success'],
              ['待签', 'text-warning'],
              ['续签', 'text-primary'],
              ['过期', 'text-error']
            ])
            return <span class={textColor.get(text)}>{text}</span>
          }
        },
        {
          title: '起租日期',
          align: 'center',
          dataIndex: 'StartDate',
          customRender: (text) => moment(text).format('YYYY-MM-DD')
        },
        {
          title: '止租日期',
          align: 'center',
          dataIndex: 'EndDate',
          customRender: (text) => moment(text).format('YYYY-MM-DD')
        },
        {
          title: '签订日期',
          align: 'center',
          dataIndex: 'SignDate',
          customRender: (text) => moment(text).format('YYYY-MM-DD')
        },
        {
          title: '操作',
          align: 'center',
          width: '80px',
          fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getContractStatistics(requestParameters).then((res) => res)
      }
    }
  },
  created () {
    this.fetchData()
  },
  methods: {
    fetchData () {
      this.searchLoading = true
      const getContractCategory = getDropdownList({
        PmKey: 'ContractCategory'
      })
      const getContractStatus = getDropdownList({
        PmKey: 'ContractStatus'
      })
      Promise.all([getContractCategory, getContractStatus])
        .then((res) => {
          this.searchList[0].props.options = res[0].map((e) => {
            return {
              value: e.DROPVALUE,
              label: e.DROPVALUE
            }
          })
          this.searchList[1].props.options = res[1].map((e) => {
            return {
              value: e.DROPVALUE,
              label: e.DROPVALUE
            }
          })
        })
        .finally(() => {
          this.searchLoading = false
        })
    },
    handleSearch (values) {
      this.queryParam.strContent = values
      this.tableRefresh(true)
    },
    handleReset () {
      this.queryParam.strContent = {}
      this.tableRefresh(true)
    },
    tableRefresh (refresh = false) {
      this.$refs.table.refresh(refresh)
    },
    handleDetail (record) {
      this.contract = record
      this.visibleDetail = true
    },
    handleSignContract (record) {
      this.contract = record
      this.visibleSign = true
    }
  }
}
</script>
<style lang="less" scoped>
// /deep/ .ant-table td {
//   white-space: nowrap;
// }
</style>
